//
//  MainAdvertView.m
//  CCBShop
//
//  Created by lijianhua on 14-11-27.
//  Copyright (c) 2014年 CCB. All rights reserved.
//

#import "MainAdvertView.h"

@implementation MainAdvertView

-(id)initWithFrame:(CGRect)frame advertsArray:(NSMutableArray *)advertsArr
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = YES;
        
        width = frame.size.width;
        height = frame.size.height;
        
        advertsArray = advertsArr;
        
        //3,0,1,2,3,0
        NSMutableArray *tempAdvertsArr = [NSMutableArray arrayWithArray:advertsArray];
        [tempAdvertsArr insertObject:[advertsArray objectAtIndex:([advertsArray count]-1)] atIndex:0];//3
        [tempAdvertsArr addObject:[advertsArray objectAtIndex:0]];
        loopAdvertsArr = tempAdvertsArr;
        NSUInteger imageCount = [loopAdvertsArr count];//n+2
        
        if ([advertsArray count] > 6)
        {
            //5,0,1,2,3,4,5,0
            NSRange range = NSMakeRange(0, 6);
            tempAdvertsArr = [NSMutableArray arrayWithArray:[advertsArray subarrayWithRange:range]];
            [tempAdvertsArr insertObject:[advertsArray objectAtIndex:5] atIndex:0];//3
            [tempAdvertsArr addObject:[advertsArray objectAtIndex:0]];
            loopAdvertsArr = tempAdvertsArr;
            imageCount = [loopAdvertsArr count];//n+2
        }
        
        //初始化scrollView
        self.imageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        self.imageScrollView.pagingEnabled = YES;//滑到subview的边界
        self.imageScrollView.bounces = NO;//边界
        self.imageScrollView.showsVerticalScrollIndicator = NO;//不显示垂直滚动条
        self.imageScrollView.showsHorizontalScrollIndicator = NO;//不显示水平滚动条
        self.imageScrollView.delegate = self;
        self.imageScrollView.contentSize = CGSizeMake(width*imageCount, height);//设置scrollview的大小
//        self.imageScrollView.layer.cornerRadius = 4.0;
        //限制一张不允许循环滑动
        if (imageCount <= 1) {
            [self.imageScrollView setContentSize:CGSizeMake(width,height)];
        }

        //添加图片视图
        for (int i=0; i<imageCount; i++)
        {
            //创建内容对象
            CGRect iFrame = CGRectMake(width*i, frame.origin.y, width, height);
            //test
            UIControl * imageCtr = [[UIControl alloc]initWithFrame:iFrame];
            imageCtr.tag = 2000+i;
            AdvertCellView *advertCell = [[AdvertCellView alloc]initWithFrameTest:iFrame advertString:[loopAdvertsArr objectAtIndex:i]];
        
            advertCell.delegate = self;
            [self.imageScrollView addSubview:advertCell];
            [self.imageScrollView addSubview:imageCtr];
        }
        
        //设置滚动圆点
        NSUInteger pageCount = imageCount-2;//n页
        
        page = 1;
        [self.imageScrollView setContentOffset:CGPointMake(width, 0) animated:NO];
        
        [self addSubview:self.imageScrollView];
        /*[self addSubview:pageControl];*/
        
        self.pageControl = [[JHPageControl alloc] init];
        [self.pageControl setCenter: CGPointMake(mainScreenWidth*0.5, 110.0f)];
        [self.pageControl setNumberOfPages: pageCount] ;
        [self.pageControl setCurrentPage: 0];
        [self.pageControl setDefersCurrentPageDisplay: YES] ;
        [self.pageControl setType: JHPageControlTypeOnFullOffFull] ;
        [self.pageControl setOnColor: [UIColor redColor]] ;
        [self.pageControl setOffColor: [UIColor whiteColor]] ;
        [self.pageControl setIndicatorDiameter: 7.0f] ;
        [self.pageControl setIndicatorSpace: 4.0f] ;
        [self addSubview: self.pageControl];
        
        //设置NSTimer,3s
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoLoop) userInfo:nil repeats:YES];
        
        //设置背景条
        self.backgroundColor =[UIColor grayColor];
//        //设置边框
//        self.layer.borderColor = DEFAULT_BORDER_CGCOLOR;
//        self.layer.borderWidth = defaultBorderWidth;
//        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)autoLoop
{
    if (page == 0)
    {
        [self.imageScrollView setContentOffset:CGPointMake(([loopAdvertsArr count]-2)*width, 0) animated:NO];
        //pageControl.currentPage = [imageNameArr count]-1;
        page = (int)[loopAdvertsArr count]-1;
    }
    else if(page == [loopAdvertsArr count]-1)
    {
        //switchDirection = leftDirection;
        /*pageControl.currentPage = 0;*/
        self.pageControl.currentPage = 0;
        [self.imageScrollView setContentOffset:CGPointMake(width, 0) animated:NO];
    }
    else
    {
        [self.imageScrollView setContentOffset:CGPointMake(page*width, 0) animated:YES];
        page ++;
    }
}

#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)aScrollView
{
	[self.pageControl updateCurrentPageDisplay] ;
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    int currentPage = floor((sender.contentOffset.x - width / 2) / width) + 1;
    page = currentPage;//设置当前页
    
    self.pageControl.currentPage = (currentPage-1);
    [self.pageControl updateCurrentPageDisplay] ;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
    if (page == 0) {
        [self.imageScrollView setContentOffset:CGPointMake(([loopAdvertsArr count]-2)*width, 0) animated:NO];
    }
    if (page == [loopAdvertsArr count]-1) {
        [self.imageScrollView setContentOffset:CGPointMake(1*width, 0) animated:NO];
    }
}

#pragma mark --AdvertCellClickDelegate
- (void)advertCellClickWithName:(NSString *)name url:(NSString *)url
{
    if (self.aDelegate && [self.aDelegate respondsToSelector:@selector(advertClickWithName:url:)]) {
        [self.aDelegate advertClickWithName:name url:url];
    }
}
@end
